const app = getApp()
//引入插件：微信同声传译..
const plugin = requirePlugin('WechatSI');
//获取全局唯一的语音识别管理器recordRecoManager
const manager = plugin.getRecordRecognitionManager();

Page({
  data: {
    cardCur: 0,
    inputValue: "",
    swiperList: [{
      id: 0,
      type: 'image',
      img: '/images/sorting.jpeg'
    },{
        id: 1,
        type: 'image',
        img: '/images/Recyclable.png'
    }, {
      id: 2,
      type: 'image',
      img: '/images/KitchenWaste.png'
    }, {
      id: 3,
      type: 'image',
      img: '/images/HarmfulWaste.png'
    }, {
      id: 4,
      type: 'image',
      img: '/images/OtherWaste.png'
    }],
    //语音
    recordState: false, //录音状态
    info:'',//API传来的数据
  },

  swipClick: function (e) {//点击图片触发事件
    let let_this = this;
    let let_index = let_this.data.cardCur;
    console.log(let_index);
    let urlList = [
      {
        url: '/pages/index/introA'
      },{
        url: '/pages/index/introB'
      },{
        url: '/pages/index/introC'
      },{
        url: '/pages/index/introD'
      },{
        url: '/pages/index/introE'
      }
    ],
    let_url = urlList[let_index].url;
    console.log(let_url);
    if(let_url != ''){
      wx.navigateTo({
        url: let_url,
      })
    }
  },


  onLoad() {
    this.towerSwiper('swiperList');
    // 初始化towerSwiper 传已有的数组名即可

    //识别语音
    this.initRecord();
  },
  DotStyle(e) {
    this.setData({
      DotStyle: e.detail.value
    })
  },
  // cardSwiper
  cardSwiper(e) {
    this.setData({
      cardCur: e.detail.current,
    })
  },
  // towerSwiper
  // 初始化towerSwiper
  towerSwiper(name) {
    let list = this.data[name];
    for (let i = 0; i < list.length; i++) {
      list[i].zIndex = parseInt(list.length / 2) + 1 - Math.abs(i - parseInt(list.length / 2))
      list[i].mLeft = i - parseInt(list.length / 2)
    }
    this.setData({
      swiperList: list
    })
  },
  // towerSwiper触摸开始
  towerStart(e) {
    this.setData({
      towerStart: e.touches[0].pageX
    })
  },
  // towerSwiper计算方向
  towerMove(e) {
    this.setData({
      direction: e.touches[0].pageX - this.data.towerStart > 0 ? 'right' : 'left'
    })
  },
  // towerSwiper计算滚动
  towerEnd(e) {
    let direction = this.data.direction;
    let list = this.data.swiperList;
    if (direction == 'right') {
      let mLeft = list[0].mLeft;
      let zIndex = list[0].zIndex;
      for (let i = 1; i < list.length; i++) {
        list[i - 1].mLeft = list[i].mLeft
        list[i - 1].zIndex = list[i].zIndex
      }
      list[list.length - 1].mLeft = mLeft;
      list[list.length - 1].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    } else {
      let mLeft = list[list.length - 1].mLeft;
      let zIndex = list[list.length - 1].zIndex;
      for (let i = list.length - 1; i > 0; i--) {
        list[i].mLeft = list[i - 1].mLeft
        list[i].zIndex = list[i - 1].zIndex
      }
      list[0].mLeft = mLeft;
      list[0].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    }
  },

  action: function(e){
    wx.navigateTo({
      url: '/pages/index/sorting',
    })
  },

  jsvalue: function(e){
    this.setData({inputValue: e.detail.value});
    app.globalData.inputValue = e.detail.value;
  },

  
  searchButton: function(e){
    //wx.setStorageSync('inputValue', this.data.inputValue);
    wx.switchTab(
      {
        url: '../index/sorting'
      }
    )

  },

  tipAFunction: function(){
    console.log("实现跳转页面");
    wx.navigateTo({
      url: '../Tips/TipA',
    })
  },

  tipBFunction: function(){
    console.log("实现跳转页面");
    wx.navigateTo({
      url: '../Tips/TipB',
    })
  },

  tipCFunction: function(){
    console.log("实现跳转页面");
    wx.navigateTo({
      url: '../Tips/TipC',
    })
  },

  toCamera:function(){
    console.log("拍照搜索");
    wx.navigateTo({
      url: '../camera/photo',
    })
  },

  //识别语音 -- 初始化
  initRecord: function () {
    const that = this;
    // 有新的识别内容返回，则会调用此事件
    manager.onRecognize = function (res) {
      console.log(res)
    }
    // 正常开始录音识别时会调用此事件
    manager.onStart = function (res) {
      console.log("成功开始录音识别", res)
    }
    // 识别错误事件
    manager.onError = function (res) {
      console.error("error msg", res)
    }
    //识别结束事件
    manager.onStop = function (res) {
      console.log('..............结束录音')
      console.log('录音临时文件地址 -->' + res.tempFilePath); 
      console.log('录音总时长 -->' + res.duration + 'ms'); 
      console.log('文件大小 --> ' + res.fileSize + 'B');
      console.log('语音内容 --> ' + res.result);
      if (res.result == '') {
        //没识别出文字
        wx.showModal({
          title: '提示',
          content: '听不清楚，请重新说一遍！',
          showCancel: false,
          success: function (res) {}
        })
        return;
      }else{
        //如果识别出文字
        var itemWithComma = res.result
        let index = itemWithComma.indexOf('。')
        var item = itemWithComma.slice(0, index)
        var that = this
        wx.request({
          url: 'http://api.tianapi.com/txapi/lajifenlei/index?key=436036e6177d1b5f93c17e7932962bf2&word=' + item,
          success: function (res) {
            console.log(res.data)
            let datamsg=JSON.stringify(res.data);
            wx.navigateTo({
              url: '../voice/voice?datamsg=' + datamsg,
            })
          }
        })
      }
      // var text = that.data.content + res.result;
      // that.setData({
      //   content: info
      // })
    }
  },
  //语音  --按住说话
  touchStart: function (e) {
    wx.showToast({
      title: '开始录音',
      icon: 'none',
      duration: 1000
    })
    this.setData({
      recordState: true  //录音状态
    })
    // 语音开始识别
    manager.start({
      lang: 'zh_CN',// 识别的语言，目前支持zh_CN en_US zh_HK sichuanhua
    })
  },
  //语音  --松开结束
  touchEnd: function (e) {
    wx.showToast({
      title: '录音结束',
      icon: 'success',
      duration: 1000
    })
    this.setData({
      recordState: false
    })
    // 语音结束识别
    manager.stop();
  },
  
})